package com.hsy.crm.web.utils;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV2Request;

import java.util.HashMap;
import java.util.Map;

public class SmsUtils {

    // 相关参数定义
    private static final String endPoint = "http://sms.bj.baidubce.com"; // SMS服务域名，可根据环境选择具体域名
    private static final String accessKeyId = "ec5b511fc64e4700a810df084d02a5ac";  // 发送账号安全认证的Access Key ID
    private static final String secretAccessKy = "cb5027e16a7e43758c9d75e53c6a614e"; // 发送账号安全认证的Secret Access Key
    private static final String invokeId = "XrjF7hJ4-6CX7-3oc8"; // 发送使用签名的调用ID
    private static final String templateCode = "smsTpl:54a56d4d-726d-445f-b5d4-86ea526952c0"; // 本次发送使用的模板Code

    public static void send(String phoneNumber,String code) {
        //sendLimit(phoneNumber);
        // ak、sk等config
        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy));
        config.setEndpoint(endPoint);

        // 实例化发送客户端
        SmsClient smsClient = new SmsClient(config);

        // 定义请求参数

        Map<String, String> vars = new HashMap(); // 若模板内容为：您的验证码是${code},在${time}分钟内输入有效
        vars.put("code", code);
        vars.put("time", "30");

        //实例化请求对象
        SendMessageV2Request request = new SendMessageV2Request();
        request.withInvokeId(invokeId)
                .withPhoneNumber(phoneNumber)
                .withTemplateCode(templateCode)
                .withContentVar(vars);

        // 发送请求
        smsClient.sendMessage(request);
    }

    private static void sendLimit(String phoneNumber){
        String key = "regcode-"+phoneNumber;
        Object obj = ExpiryMap.get(key);
        if( obj == null ){
            ExpiryMap.put(key,1);
        }else{
            int index = (int)obj;

            ExpiryMap.put(key,index);
        }

    }

}
